#!/bin/sh
# group.sh
# 
#
# Created by [CGM]sgxiao on 09-01-30.
# Copyright 2008 CGM. All rights reserved.




startYear=1994
endYear=2008
DATA=data
RESULT=result.doc
filename=$1
AWKSRC=group.awk
PRICEVOLUME=temp.group
PRICEVOLUME1=group

#mkdir ${PRICEVOLUME1}

#the nearest available date of Jan
header[1994]="0103"
header[1995]="0103"
header[1996]="0102"
header[1997]="0102"
header[1998]="0105"
header[1999]="0104"
header[2000]="0104"
header[2001]="0102"
header[2002]="0104"
header[2003]="0102"
header[2004]="0102"
header[2005]="0104"
header[2006]="0104"
header[2007]="0104"
header[2008]="0102"

while [ ${startYear} -le ${endYear} ] 
do
	echo ${startYear}
echo "BEGIN {
	FS=\"\\\\t\"
}
\$1 ~ /${startYear}${header[startYear]}/{
	print \$2, \$(15)
}
END{}
" > ${AWKSRC}

	awk -f ${AWKSRC} ${startYear}.${DATA} >  ${startYear}.${PRICEVOLUME}
	num=`wc -l ${startYear}.${PRICEVOLUME} | awk '{print $1}'`
	echo "num is $num"
	java MarketCapTop50 ${startYear}.${PRICEVOLUME} ${num} >  ${startYear}.${PRICEVOLUME1}
	startYear=$((startYear + 1))
done

./divide.sh

rm *.${PRICEVOLUME}
rm *.${PRICEVOLUME1}

mv *.data ../